//
//  KWTMGRNSettingDateCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/8/2.
//

#import "KWTMGRNSettingDateCell.h"
@interface KWTMGRNSettingDateCell()

@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic, strong) QMUIButton *setBtn;

@property (nonatomic, strong) QMUIButton *refreshBtn;

@property (nonatomic, strong) QMUITextField *textField;

@end
@implementation KWTMGRNSettingDateCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

-(void)setModel:(KWTMGRNSettingModel *)model
{
    _model = model;
  
    self.titleLab.text = model.rowTitle.rh_localized;

    self.textField.text = model.content;
    //转成当地时区的时间
//    self.textField.text = [NSDate beiJinTimeConvertToLocalTime:model.content];
}

-(void)hideRefreshButton
{
    self.refreshBtn.hidden = YES;
}

-(void)setupUI
{
    kWeakSelf;
    self.contentView.backgroundColor = kCOLOR_HEX(@"#F0F5FF");
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"" font:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#171A1D")];
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(15);
    }];
    
    _setBtn = [[QMUIButton alloc]init];
    _setBtn.titleLabel.font = kSYSTEMFONT(14);
    [_setBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [_setBtn setTitle:@"设置".rh_localized forState:UIControlStateNormal];
    _setBtn.backgroundColor = CMainColorGreen;
    [_setBtn setCornerRadius:8];
    [bgView addSubview:_setBtn];
    CGFloat btnW = [_setBtn.titleLabel.text getStringWidthFont:_setBtn.titleLabel.font limitHeight:30]+30;
    [_setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.height.offset(32);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(15);
        make.width.offset(btnW);
    }];
    
    _textField = [[QMUITextField alloc]init];
    _textField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    _textField.userInteractionEnabled = YES;
    _textField.layer.cornerRadius = 5;
    _textField.layer.masksToBounds = YES;
    _textField.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    _textField.font = kSYSTEMFONT(14);
    _textField.placeholder = @"请选择".rh_localized;
    _textField.textColor = kCOLOR_HEX_A(@"#171A1D", 0.6);
    [bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.height.equalTo(weakSelf.setBtn);
        make.centerY.equalTo(weakSelf.setBtn);
        make.right.equalTo(weakSelf.setBtn.mas_left).offset(-15);
        make.bottom.mas_offset(-10);
    }];
    
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    _textField.rightView = btnView;
    _textField.rightViewMode = UITextFieldViewModeAlways;
    
    QMUIButton *refreshBtn = [[QMUIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [refreshBtn setImage:[UIImage imageNamed:@"refresh"] forState:UIControlStateNormal];
    [bgView addSubview:refreshBtn];
    self.refreshBtn = refreshBtn;
    [refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(30);
        make.right.equalTo(weakSelf.textField);
        make.centerY.equalTo(weakSelf.textField);
    }];
    
    QMUIButton *button = [[QMUIButton alloc]init];
    [bgView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(weakSelf.textField);
        make.right.equalTo(refreshBtn.mas_left).offset(-5);
    }];
    
    
    //时间选择
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.selectAction) {
            weakSelf.selectAction(weakSelf.model);
        }
    }];
    
    //时间设置
    [[_setBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.setAction) {
            weakSelf.setAction(weakSelf.model);
        }
    }];
    
    //时间读取
    [[refreshBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.readAction) {
            weakSelf.readAction(weakSelf.model);
        }
    }];
}
@end
